<script setup>
  import { ref } from 'vue'
  import { delPatientInfoApi, getpatientInfoListApi } from '@/server/patient.js'
  import { onShow } from '@dcloudio/uni-app'

  // 右侧标题信息
  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
      },
    },
  ])

  // 获取患者列表信息
  const isShow = ref(false)
  const patientInfoList = ref([])
  async function getPatientInfoList() {
    try {
      const { data } = await getpatientInfoListApi()
      const res = data.data.map((item) => {
        const startsix = item.idCard.slice(0, 6)
        const middle = '*'.repeat(8)
        const lastPart = item.idCard.slice(14)
        item.idCard = startsix + middle + lastPart
        return item
      })
      patientInfoList.value = res
      isShow.value = true
    } catch (e) {
      //TODO handle the exception
      console.log(e)
    }
  }
  // 每次页面显示时获取患者列表更新数据
  onShow(() => {
    getPatientInfoList()
  })

  // 删除患者
  const isPopShow = ref(false)
  // 患者id
  const delId = ref('')
  function onDelPatientSubmitFn(ev) {
    delId.value = ev
    isPopShow.value = true
  }
  // 删除患者
  async function ondelPatientfn() {
    const res = await delPatientInfoApi(delId.value)
    getPatientInfoList()
    isPopShow.value = false
  }

  //
</script>

<template>
  <scroll-page v-if="isShow">
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>
      <uni-swipe-action>
        <uni-swipe-action-item
          :right-options="swipeOptions"
          v-on:click="onDelPatientSubmitFn(item.id)"
          v-for="(item, index) in patientInfoList"
          v-bind:key="item.id"
        >
          <view class="archive-card active">
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{ item.idCard }}</text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="'/subpackPatient/from/index?id=' + item.id"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-show="patientInfoList.length < 6" class="archive-card">
        <navigator
          class="add-link"
          hover-class="none"
          url="/subpackPatient/from/index"
        >
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
  </scroll-page>

  <!-- 弹出框 -->
  <up-modal
    :show="isPopShow"
    width="300px"
    title="删除"
    content="您确认要删除吗"
    showCancelButton
    @confirm="ondelPatientfn"
    @cancel="isPopShow = false"
  ></up-modal>
</template>

<style lang="scss">
  @import './index.scss';
</style>
